package com.zuimei.zuimei.activities;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.base.MySwipeBackActivity;
import com.zuimei.zuimei.beans.CollectionsBean;
import com.zuimei.zuimei.beans.FindAppItem2Bean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;
import com.zuimei.zuimei.utils.ConstantUtil;
import com.zuimei.zuimei.utils.ScreenUtil;
import com.zuimei.zuimei.utils.ShareSdkUtil;
import com.zuimei.zuimei.utils.URLImageGetter;
import com.zuimei.zuimei.widgets.CircleImageView;
import com.zuimei.zuimei.widgets.DownLoadDialog;
import com.zuimei.zuimei.widgets.MyScrollView;
import com.zuimei.zuimei.widgets.XCFlowLayout;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Jogger on 2017/2/3.
 * 收藏详情
 */

public class CollectionsDescActivitiy extends MySwipeBackActivity {
    //2
    CircleImageView iv_avatar;
    TextView tv_flowers;
    TextView tv_user_name;
    TextView tv_report;
    TextView tv_career;
    XCFlowLayout flowlayout;
    LinearLayout ll_head_container;

    //-------------1
    ImageView iv_head;
    @Bind(R.id.iv_edxq_sm)
    ImageView iv_edxq_sm;
    @Bind(R.id.tv_edxq_title)
    TextView tv_edxq_title;
    @Bind(R.id.tv_edxq_sub)
    TextView tv_edxq_sub;
    @Bind(R.id.rl_head_container)
    RelativeLayout rl_head_container;
    LinearLayout ll_tags_container;
    @Bind(R.id.ll_btn_container)
    LinearLayout ll_btn_container;
    @Bind(R.id.bt_edxq_fav)
    Button bt_edxq_fav;
    @Bind(R.id.bt_edxq_share)
    Button bt_edxq_share;
    @Bind(R.id.bt_edxq_down)
    Button bt_edxq_down;
    @Bind(R.id.tv_desc)
    TextView tv_desc;
    @Bind(R.id.ll_desc_img_container)
    LinearLayout ll_desc_img_container;
    @Bind(R.id.tv_edxq_tui)
    TextView tv_edxq_tui;
    @Bind(R.id.sv_content_container)
    MyScrollView sv_content_container;
    @Bind(R.id.ibtn_back)
    ImageButton ibtn_back;
    @Bind(R.id.ibtn_app)
    ImageButton ibtn_app;
    @Bind(R.id.iv_flower)
    ImageView iv_flower;
    @Bind(R.id.ll_download)
    LinearLayout ll_download;
    //@Bind(R.id.iv_edxq_deng)
    ImageView iv_edxq_deng;
    @Bind(R.id.ibtn_fav)
    ImageButton ibtn_fav;
    @Bind(R.id.ibtn_share)
    ImageButton ibtn_share;
    @Bind(R.id.ibtn_down)
    ImageButton ibtn_down;
    private int y;
    private int mY;
    private int mShowY;

    private CollectionsBean mCollections;
    private FindAppItem2Bean mDesc;

    @Override
    public int getLayoutId() {
        mCollections = (CollectionsBean) getIntent().getSerializableExtra
                ("collections");
        if ("每日最美".equals(mCollections.getSource_msg())) {
            return R.layout.activity_item1_desc;
        }
        if ("发现应用".equals(mCollections.getSource_msg())) {
            return R.layout.activity_item2_desc;
        }
        return 0;
    }

    @Override
    protected void init() {
        rl_head_container.measure(0, 0);
        ibtn_back.measure(0, 0);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        mY = ScreenUtil.getSrceenHeight(getBaseContext());
        int[] position = new int[2];
        ibtn_app.getLocationOnScreen(position);
        y = position[1];
    }

    @Override
    protected void loadData() {
        new HttpHelper(this).getData(String.format(ConstantUtil.USER_COLLECT_DESC_URL,
                mCollections.getId()), new HttpHelper.OnLoadCompletedListener() {

            @Override
            public void onSuccess(String result) {
                mDesc = ParseFindAppJson.getCollectionsDesc(result);

                Picasso.with(getBaseContext())
                        .load(mDesc.getAuthor_avatar_url())
                        .config(Bitmap.Config.RGB_565)
                        .into(iv_edxq_sm);
                tv_edxq_title.setText(mDesc.getTitle());
                String subTitle = mDesc.getSub_title().length() > 16 ? mDesc.getSub_title()
                        .substring(0, 16) + "..." : mDesc.getSub_title();
                tv_edxq_sub.setText(subTitle);
                tv_desc.setMovementMethod(LinkMovementMethod.getInstance());//加这句才能让里面的超链接生效
                tv_desc.setText(Html.fromHtml(mDesc.getDescription(), new URLImageGetter
                        (CollectionsDescActivitiy.this, tv_desc), null));
                Picasso.with(getBaseContext())
                        .load(mDesc.getIcon_image())
                        .config(Bitmap.Config.RGB_565)
                        .into(iv_edxq_sm);
                if (mDesc.getAll_images().size() != 0) {
                    for (int i = 0; i < mDesc.getAll_images().size(); i++) {
                        ImageView imageView = new ImageView(getBaseContext());
                        Picasso.with(getBaseContext())
                                .load(mDesc.getAll_images().get(i))
                                .config(Bitmap.Config.RGB_565)
                                .into(imageView);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup
                                .LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        params.topMargin = 20;
                        imageView.setLayoutParams(params);
                        ll_desc_img_container.addView(imageView);
                    }
                }
                //2
                if ("发现应用".equals(mCollections.getSource_msg())) {
                    initItem2View();
                    Picasso.with(getBaseContext())
                            .load(mDesc.getAuthor_avatar_url())
                            .config(Bitmap.Config.RGB_565)
                            .into(iv_avatar);
                    tv_career.setText(mDesc.getAuthor_career());
                    tv_flowers.setText(mDesc.getAuthor_flowers() + "");
                    if (mDesc.getTags().size() != 0) {
                        for (int i = 0; i < mDesc.getTags().size(); i++) {
                            CardView cardView = new CardView(getBaseContext());
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout
                                    .LayoutParams
                                    .WRAP_CONTENT);
                            cardView.setRadius(30);
                            TextView textView = new TextView(getBaseContext());
                            params.leftMargin = 20;
                            params.topMargin = 1;
                            params.bottomMargin = 10;
                            params.rightMargin = 1;
                            cardView.setLayoutParams(params);
                            textView.setLayoutParams(params);
                            textView.setPadding(50, 10, 50, 10);
                            textView.setTextColor(Color.BLACK);
                            textView.setText(mDesc.getTags().get(i).getTitle());
                            cardView.addView(textView);
                            flowlayout.addView(cardView);
                        }
                    }
                }
                //1
                else if ("每日最美".equals(mCollections.getSource_msg())) {
                    initItem1View();
                    Picasso.with(CollectionsDescActivitiy.this)
                            .load(mDesc.getCover_image())
                            .into(iv_head, new Callback() {
                                @Override
                                public void onSuccess() {
                                    sv_content_container.setEnlarge(iv_head);
                                }

                                @Override
                                public void onError() {

                                }
                            });
                    Picasso.with(CollectionsDescActivitiy.this)
                            .load(mDesc.getAuthor_avatar_url())
                            .into(iv_edxq_deng);

                }

                sv_content_container.setOnScrollListener(mShowY + ibtn_back.getMeasuredHeight(), mShowY,
                        new MyScrollView.OnScrollListener() {
                            @Override
                            public void showAppBtn() {
                                ibtn_app.animate().translationYBy(y).y(mY).setDuration(500)
                                        .setInterpolator(new BounceInterpolator()).start();
                            }

                            @Override
                            public void hideAppBtn() {
                                ibtn_app.animate().translationYBy(mY).y(y).setDuration(500)
                                        .setInterpolator(new BounceInterpolator()).start();
                            }

                            @Override
                            public void upShow() {
                                ll_btn_container.setVisibility(View.INVISIBLE);
                                ibtn_fav.setVisibility(View.VISIBLE);
                                ibtn_share.setVisibility(View.VISIBLE);
                                ibtn_down.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void downShow() {
                                //滑动到与回退按钮底部
                                ibtn_fav.setVisibility(View.INVISIBLE);
                                ibtn_share.setVisibility(View.INVISIBLE);
                                ibtn_down.setVisibility(View.INVISIBLE);
                                ll_btn_container.setVisibility(View.VISIBLE);
                            }
                        });
            }

            @Override
            public void onFailure() {

            }
        });
    }

    private void initItem1View() {
//        View view = LayoutInflater.from(this).inflate(R.layout.activity_item1_desc, null);
        iv_head = (ImageView) findViewById(R.id.iv_head);

        ll_tags_container = (LinearLayout) findViewById(R.id.ll_tags_container);
        iv_edxq_deng = (ImageView)findViewById(R.id.iv_edxq_deng);
        mShowY = 360 + rl_head_container.getMeasuredHeight() - ibtn_back.getMeasuredHeight();
    }

    private void initItem2View() {
        Log.d("TAG", "-----2");
        View view = LayoutInflater.from(this).inflate(R.layout.activity_item2_desc, null);
        iv_avatar = (CircleImageView) view.findViewById(R.id.iv_avatar);
        tv_flowers = (TextView) view.findViewById(R.id.tv_flowers);
        tv_user_name = (TextView) view.findViewById(R.id.tv_user_name);
        tv_report = (TextView) view.findViewById(R.id.tv_report);
        tv_career = (TextView) view.findViewById(R.id.tv_career);
        flowlayout = (XCFlowLayout) view.findViewById(R.id.flowlayout);
        ll_head_container = (LinearLayout) view.findViewById(R.id.ll_head_container);
        ll_head_container.measure(0, 0);
        mShowY = ll_head_container.getMeasuredHeight() + rl_head_container.getMeasuredHeight();
    }

    @OnClick({R.id.ibtn_back, R.id.ll_download, R.id.bt_edxq_down, R.id.ibtn_down, R.id
            .bt_edxq_share, R.id.ibtn_share})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.ll_download:
            case R.id.bt_edxq_down:
            case R.id.ibtn_down:
                download();
                break;
            case R.id.bt_edxq_share:
            case R.id.ibtn_share:
                new ShareSdkUtil().showShare(this);
                break;
        }
    }

    private void download() {
        DownLoadDialog dialog = new DownLoadDialog();
        Bundle bundle = new Bundle();
        bundle.putString("title", mDesc.getTitle());
        bundle.putString("size", mDesc.getSize());
        if (mDesc.getDownload_urls().size() != 0) {
            for (int i = 0; i < mDesc.getDownload_urls().size(); i++) {
                String name = mDesc.getDownload_urls().get(i).getName();
                if ("豌豆荚".equals(name)) {
                    bundle.putString("wandoujia", mDesc.getDownload_urls().get(i)
                            .getUrl());
                }
                if ("Google Play".equals(name)) {
                    bundle.putString("wandoujia", mDesc.getDownload_urls().get(i)
                            .getUrl());
                }
                if ("直接".equals(name)) {
                    bundle.putString("direct", mDesc.getDownload_urls().get(i)
                            .getUrl());
                }
            }
        }
        dialog.setArguments(bundle);
        dialog.show(getFragmentManager(), "download_dialog");
    }
}
